package com.usian.usianleadonewsuser.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.medis.NewsAutoScanConstants;
import com.usian.model.article.ApAuthor;
import com.usian.model.behavior.dto.FollowBehaviorDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.UserRelationDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.usianleadonewsuser.feing.ArticleFeing;
import com.usian.usianleadonewsuser.mapper.ApUserMapper;
import com.usian.usianleadonewsuser.mapper.AuthUserFanMapper;
import com.usian.usianleadonewsuser.mapper.AuthUserFollowMapper;
import com.usian.usianleadonewsuser.service.AuthUserService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class AuthUserServiceImpl implements AuthUserService {
    @Autowired
    private AuthUserFollowMapper authUserFollowMapper;

    @Resource
    private AuthUserFanMapper authUserFanMapper;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Autowired
    private ArticleFeing articleFeing;

    @Resource
    private ApUserMapper apUserMapper;

    @Override
    public ResponseResult user_follow(UserRelationDto dto) {
        //检验参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getOperation()==0){
            ApUser user = AppThreadLocalUtils.getUser();
            //根据作者id进行查询
            ApAuthor byidApAuthor = articleFeing.findByidApAuthor(dto.getAuthorId());
            LambdaQueryWrapper<ApUserFollow> q = new LambdaQueryWrapper<>();
            q.eq(ApUserFollow::getUserId,user.getId());
            q.eq(ApUserFollow::getFollowId,byidApAuthor.getId());
            ApUserFollow apUserFollow1 = authUserFollowMapper.selectOne(q);

            if(apUserFollow1==null){
                //封装ap_user_follow表数据
                ApUserFollow apUserFollow = new ApUserFollow();
                apUserFollow.setFollowId(byidApAuthor.getId());
                apUserFollow.setFollowName(byidApAuthor.getName());
                apUserFollow.setCreatedTime(new Date());
                apUserFollow.setIsNotice(true);
                apUserFollow.setLevel(0);
                apUserFollow.setUserId(user.getId());
                authUserFollowMapper.insert(apUserFollow);
            }
            LambdaQueryWrapper<ApUserFan> qq = new LambdaQueryWrapper<>();
            qq.eq(ApUserFan::getUserId,byidApAuthor.getUserId());
            qq.eq(ApUserFan::getFansId,user.getId());
            ApUserFan apUserFan1 = authUserFanMapper.selectOne(qq);

            if(apUserFan1==null){
                ApUserFan apUserFan = new ApUserFan();
                apUserFan.setUserId(byidApAuthor.getUserId());
                apUserFan.setCreatedTime(new Date());
                apUserFan.setFansId(user.getId());
                ApUser apUser = apUserMapper.selectById(user.getId());
                apUserFan.setFansName(apUser.getName());
                apUserFan.setLevel(0);
                apUserFan.setIsDisplay(true);
                apUserFan.setIsShieldComment(true);
                apUserFan.setIsShieldLetter(true);
                authUserFanMapper.insert(apUserFan);
            }
            FollowBehaviorDto followBehaviorDto = new FollowBehaviorDto();
            followBehaviorDto.setArticleId(dto.getArticleId());
            followBehaviorDto.setFollowId(dto.getAuthorId());
            followBehaviorDto.setUserId(user.getId());
            kafkaTemplate.send(NewsAutoScanConstants.AP_BEHAVIOR,JSON.toJSONString(followBehaviorDto));
        }else{
            //刪除ApUserFollow表
            ApUser user = AppThreadLocalUtils.getUser();
            LambdaQueryWrapper<ApUserFollow> q = new LambdaQueryWrapper<>();
            q.eq(ApUserFollow::getUserId,user.getId());
            q.eq(ApUserFollow::getFollowId,dto.getAuthorId());
            authUserFollowMapper.delete(q);

            //刪除ApUserFan表
            ApAuthor byidApAuthor = articleFeing.findByidApAuthor(dto.getAuthorId());
            LambdaQueryWrapper<ApUserFan> qq = new LambdaQueryWrapper<>();
            qq.eq(ApUserFan::getUserId,byidApAuthor.getUserId());
            qq.eq(ApUserFan::getFansId,user.getId());
            authUserFanMapper.delete(qq);
        }


        return null;
    }

    @Override
    public ApUser findById(Integer id) {
        return apUserMapper.selectById(id);
    }
}
